/*
*
* 防抖：
* 主要思想：事件响应函数在一段时间后才执行，如果在规定时间内重复触发，则重新计算时间
* */

function debounce(fun,wait,immediately){
    let timeout = null;
    let result;
    function debounced(){
        let content = this;
        let args = arguments;
        clearTimeout(timeout)
        if (immediately){
            let isTime = !timeout;
            timeout = setTimeout(() => {
                result = fun.apply(content,args);
            },wait)
            if (isTime) {
                result = fun.apply(content,args);
            }
        }else{
            timeout = setTimeout(() => {
                result = fun.apply(content,args);
            },wait)
        }
        return result;
    }

    debounced.cannce = function(){
        clearTimeout(timeout)
        //timeout = null;
    }
    return debounced;
}

let dec = document.querySelector('#dec');
let number = document.querySelector('#number');
let add = document.querySelector('#add');
let container = document.querySelector('#container');
let cannce = document.querySelector('#cannce');
let decb = debounce(function(e){
    number.innerHTML = Number(number.innerHTML) - 1;
},1000,true);
let addb = debounce(function(e){
    number.innerHTML = Number(number.innerHTML) + 1;
},3000,true);
dec.addEventListener("click",decb)
container.addEventListener('mousemove',addb)
add.addEventListener('click',addb)
cannce.addEventListener('click',addb.cannce);
